package LandR.personal.personalServlet;


import LandR.personal.personalHelp.Users.User;
import LandR.personal.personalHelp.function_json.Util;
import LandR.personal.personalHelp.function_jsp.JspPageUseMethod;
import token.TokenHelp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

/**
 * 这个servlet控制着在个人界面开启之前返回数据库中存储的个人信息的值
 */
@WebServlet("/BeforePageServlet")
public class BeforePageServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/json;charset=utf-8"); // 响应文本格式， json格式
        response.setCharacterEncoding("utf-8"); // 编码格式
        request.setCharacterEncoding("utf-8");

        if (request.getMethod().equals("GET")) {
            /*
            token获取
             */
            String token = request.getHeader("token");

            token = token.substring(token.indexOf("\"") + 1, token.lastIndexOf("\""));
            System.out.println("前端传递的token令牌是：" + token);
            TokenHelp th = new TokenHelp();
            Integer ID = -1;

            try {
                ID = th.judgeToken(token);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }


            if (ID != -1) {
                User user = new User();
                user.setId(ID);

                JspPageUseMethod jpum = new JspPageUseMethod();
                PrintWriter out = null;
                try {
                    User getUser = jpum.selectUserData(user);
                    jpum.distDate(getUser);
                    jpum.distArea(getUser);
                    Util util = new Util();
                    String json = util.javaToJsonObjectByFastJson(getUser);

                    out = response.getWriter();
                    out.print(json);
                    System.out.println("查询信息流已发送给前端");
                    out.flush();
                } catch (SQLException e) {
                    e.printStackTrace();
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                } finally {
                    out.close();
                }
            } else {
                PrintWriter out = response.getWriter();
                out.print("失败！token已过期或token不匹配！");
                System.out.println("失败！token已过期或token不匹配！");
            }

        } else {
            PrintWriter out = response.getWriter();
            System.out.println("失败！查询方法不匹配！");
            out.println("失败！查询方法不匹配！");

        }

    }
}
